<!-- 交易明细 -->
<script lang="ts" setup>
import { ref, watch, reactive } from "vue"
import { Search, Refresh, Download } from "@element-plus/icons-vue"
import { type FormInstance } from "element-plus"
import { moneyQuery, transactionDetail } from "@/api/vouchercenter/index"
import { usePagination } from "@/hooks/usePagination"
import { formatDateTime } from "@/utils/index"
import { useUserStore } from "@/store/modules/user"
import { shortcuts } from "@/utils/dateSet"
defineOptions({
  name: "transactionDetail"
})
const userStore = useUserStore()
const loading = ref<boolean>(false)
const { paginationData, handleCurrentChange, handleSizeChange } = usePagination()
const tableData = ref<any>()
const searchData = reactive<any>({
  data: "",
  state: "",
  moneyId: "", // 订单号
  note: "" // 充值账号
})

// 查
const total = ref()
const getmoneyQuery = () => {
  if (!searchData.data) {
    searchData.data = []
  }
  moneyQuery({
    page: paginationData.currentPage,
    size: paginationData.pageSize,
    id: userStore.usernameID,
    start: new Date(searchData?.data[0]).getTime() || "",
    end: new Date(searchData?.data[1]).getTime() || "",
    note: searchData.note,
    moneyId: searchData.moneyId
  }).then((res: any) => {
    tableData.value = res.data.list
    total.value = res.data.total
    console.log(res, "123456")
  })
}

/** 监听分页参数的变化 */
watch([() => paginationData.currentPage, () => paginationData.pageSize], getmoneyQuery, { immediate: true })
// 搜索
const searchFormRef = ref<FormInstance | null>(null)

const handleSearch = () => {
  if (paginationData.currentPage === 1) {
    getmoneyQuery()
  }
  paginationData.currentPage = 1
}
const resetSearch = () => {
  searchFormRef.value?.resetFields()
  getmoneyQuery()
}
/** 订单下载 默认下载所有数据，查询后下载所有符合查询条件的数据*/
const downLoad = async () => {
  loading.value = true
  console.log("交易明细下载")
  await transactionDetail({
    id: userStore.usernameID,
    start: new Date(searchData?.data[0]).getTime() || "",
    end: new Date(searchData?.data[1]).getTime() || "",
    note: searchData.note,
    moneyId: searchData.moneyId
  }).then((res: any) => {
    const blob = new Blob([res.data], {
      type: "application/xlsx;charset=utf-8"
    })
    // console.log(res)
    const downloadElement = document.createElement("a")
    const href = window.URL.createObjectURL(blob)
    downloadElement.href = href
    downloadElement.download = "交易明细.xlsx"
    document.body.appendChild(downloadElement)
    downloadElement.click()
    window.URL.revokeObjectURL(href)
    loading.value = false
  })
}
// 时间
const defaultTime1: [Date, Date] = [new Date(2000, 1, 1, 0, 0, 0), new Date(2000, 2, 1, 23, 59, 0)]
</script>

<template>
  <div class="app-container">
    <!-- 搜索功能 -->
    <el-card v-loading="loading" shadow="never" class="search-wrapper">
      <el-form ref="searchFormRef" :inline="true" :model="searchData">
        <el-form-item prop="data" label="日期">
          <el-date-picker
            v-model="searchData.data"
            type="datetimerange"
            range-separator="到"
            start-placeholder="开始时间"
            end-placeholder="结束时间"
            :shortcuts="shortcuts"
            :default-time="defaultTime1"
          />
        </el-form-item>
        <!-- <el-form-item prop="state" label="类型">
          <el-select v-model="searchData.state" clearable placeholder="请选择类型">
            <el-option label="充值扣款" value="充值扣款" />
            <el-option label="账户加款" value="账户加款" />
          </el-select>
        </el-form-item> -->
        <el-form-item prop="moneyId" label="订单号">
          <el-input v-model.trim="searchData.moneyId" placeholder="请输入订单号" />
        </el-form-item>
        <el-form-item prop="note" label="充值手机号">
          <el-input v-model.trim="searchData.note" placeholder="请输入手机号" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" :icon="Search" @click="handleSearch">查询</el-button>
          <el-button :icon="Refresh" @click="resetSearch">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <!-- 表格内容 -->
    <el-card v-loading="loading">
      <div class="toolbar-wrapper">
        <el-tooltip content="下载">
          <el-button type="primary" :icon="Download" circle @click="downLoad" />
        </el-tooltip>
      </div>
      <div class="table-center">
        <el-table :data="tableData" style="width: 100%">
          <el-table-column label="交易日期" width="180" align="center">
            <template #default="scope"> {{ formatDateTime(scope.row.date) }} </template>
          </el-table-column>
          <el-table-column prop="id" label="订单号" align="center" />
          <el-table-column prop="note" label="充值手机号" align="center" />
          <el-table-column prop="remark" label="充值内容" align="center" />
          <el-table-column prop="orderReference" label="订单编号" align="center" />

          <el-table-column label="运营商" align="center">
            <template #default="scope">
              <text v-if="scope.row.operator == '0'">中国移动 </text>
              <text v-if="scope.row.operator == '1'">中国电信 </text>
              <text v-if="scope.row.operator == '2'">中国联通 </text>
              <text v-if="scope.row.operator == '3'">河南电信 </text>
            </template>
          </el-table-column>
          <el-table-column prop="overview" label="交易概述" width="180" align="center" />
          <el-table-column label="交易金额(元)" align="center">
            <template #default="scope">
              <div :class="scope.row.amount.indexOf('+') > -1 ? 'active' : 'active1'">
                {{ scope.row.amount }}
              </div>
            </template>
          </el-table-column>
          <el-table-column prop="balance" label="账户余额(元)" align="center" />
        </el-table>
      </div>
      <!-- 分页器 -->
      <div class="pager-wrapper">
        <el-pagination
          background
          :layout="paginationData.layout"
          :page-sizes="paginationData.pageSizes"
          :total="total"
          :page-size="paginationData.pageSize"
          :currentPage="paginationData.currentPage"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>
  </div>
</template>

<style lang="scss" scoped>
.search-wrapper {
  margin-bottom: 20px;
}
.toolbar-wrapper {
  display: flex;
  justify-content: space-between;
  margin-bottom: 20px;
}
.active {
  color: red;
}
.active1 {
  color: green;
}
.table-center {
  margin-bottom: 20px;
}
.pager-wrapper {
  display: flex;
  justify-content: flex-end;
}
</style>
